package com.jkc.xh.fragments;


import android.app.DatePickerDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.adapters.MoneyBillAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.FundsListResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 委托撤单
 */
@EFragment(R.layout.fragment_money_bill)
public class MoneyBillFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnItemClickListener {

    @ViewById(R.id.activityTitleName)
    NavBar activityTitleName;
    @ViewById
    TextView timeStart;
    @ViewById
    TextView timeEnd;

    private MoneyBillAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;
    private DatePickerDialog datePickerDialog;
    private DatePickerDialog datePickerDialogEnd;
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
    private long queryStart, queryEnd;
    private final int BILL_PAGE_SIZE = Constant.PAGE_SIZE_20;

    @Override
    protected void initView(PullRecyclerView recyclerView) {
        mAdapter = new MoneyBillAdapter();
        mAdapter.setItemClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);

        activityTitleName.setOnMenuClickListener(new NavBar.OnMenuClickListener() {
            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }

            @Override
            public void onRightMenuClick(View view) {
                requestData(true);
            }
        });


        Calendar time = Calendar.getInstance();
        time.set(Calendar.HOUR_OF_DAY, 23);
        time.set(Calendar.SECOND, 59);
        time.set(Calendar.MINUTE, 59);
        int year = time.get(Calendar.YEAR);
        int month = time.get(Calendar.MONTH);
        int day = time.get(Calendar.DAY_OF_MONTH);
        queryEnd = time.getTimeInMillis() / 1000;

        datePickerDialogEnd = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar time = Calendar.getInstance();
                time.set(year, monthOfYear, dayOfMonth, 23, 59, 59);
                queryEnd = time.getTimeInMillis() / 1000;
                timeEnd.setText(formatter.format(time.getTime()));
            }
        }, year, month, day);
        datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar time = Calendar.getInstance();
                time.set(year, monthOfYear, dayOfMonth, 0, 0, 0);
                queryStart = time.getTimeInMillis() / 1000;
                timeStart.setText(formatter.format(time.getTime()));
            }
        }, year, month, day-7);

        timeStart.setText(getFirstDayOfLastMonth());
        timeEnd.setText(formatter.format(new Date()));

    }

    //获取上个月
    public String getFirstDayOfLastMonth() {
        String str = "";
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.HOUR_OF_DAY, 0);
        lastDate.set(Calendar.SECOND, 0);
        lastDate.set(Calendar.MINUTE, 0);
        lastDate.add(Calendar.DAY_OF_MONTH, -7);//reduce a month to be last month
        queryStart = lastDate.getTimeInMillis() / 1000;
        str = formatter.format(lastDate.getTime());
        return str;
    }

    @AfterViews
    void initData() {
        requestData(true);
    }

    private void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            getPullRecyclerView().autoRefresh();
            mAdapter.clear();
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        if (queryStart >= queryEnd) {
            ToastUtil.showToastShort("开始时间必须比结束时间早");
            return;
        }
        addJob(TraderManager.queryFundsList(queryStart, queryEnd, pageIndex,BILL_PAGE_SIZE, new RestCallBack<FundsListResponse>() {


            @Override
            public void success(FundsListResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
                    finishLoad(false);
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < BILL_PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }
            }

            @Override
            public void failure(RestError error) {

            }
        }));

    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
//        finishLoad(true);
    }


    @Override
    public void onItemClick(View view, Object object, int position) {
        BillMoneyDetailFragment_.builder().arg("modelExtra", mAdapter.get(position)).build().startFragment(getContext());
    }

    @Click(R.id.btnloadMore)
    void loadMore() {
//        requestData(false);

    }

    @Click(R.id.timeStart)
    void clickStartTime() {
        datePickerDialog.show();
    }

    @Click(R.id.timeEnd)
    void clickEndTime() {
        datePickerDialogEnd.show();
    }

    @Click(R.id.btnQuery)
    void btnQuery() {
        requestData(true);
    }
}
